const vscode = require('vscode');
const cp = require('child_process');
const path = require('path');

function activate(context) {
    let disposable = vscode.commands.registerCommand('fbpOpener.openFbpFile', (resource) => {
        let fileUri = resource && resource.fsPath ? resource : undefined;
        if (!fileUri) {
            vscode.window.showErrorMessage('未能获取到 FBP 文件路径。');
            return;
        }
            // 读取用户设置的 wxFormBuilder 路径
            const config = vscode.workspace.getConfiguration('fbpOpener');
            let wxFormBuilderPath = config.get('wxFormBuilderPath');
            const fs = require('fs');
            // 自动检测常见路径
            const commonPaths = [
                'D:/Program Files/wxFormBuilder/wxFormBuilder.exe',
                'C:/Program Files/wxFormBuilder/wxFormBuilder.exe',
                'D:/wxFormBuilder/wxFormBuilder.exe',
                'C:/wxFormBuilder/wxFormBuilder.exe'
            ];
            function tryDetectPath() {
                for (const p of commonPaths) {
                    if (fs.existsSync(p)) {
                        return p;
                    }
                }
                return null;
            }
            // 如果配置项不存在或文件不存在，自动检测
            if (!wxFormBuilderPath || !fs.existsSync(wxFormBuilderPath)) {
                const detected = tryDetectPath();
                if (detected) {
                    wxFormBuilderPath = detected;
                    config.update('wxFormBuilderPath', wxFormBuilderPath, true);
                } else {
                    // 弹窗让用户选择 exe 文件
                    vscode.window.showOpenDialog({
                        canSelectMany: false,
                        openLabel: '选择 wxFormBuilder.exe',
                        filters: { '可执行文件': ['exe'] }
                    }).then(fileUriArr => {
                        if (fileUriArr && fileUriArr[0]) {
                            wxFormBuilderPath = fileUriArr[0].fsPath;
                            config.update('wxFormBuilderPath', wxFormBuilderPath, true);
                            cp.execFile(wxFormBuilderPath, [fileUri.fsPath], (err) => {
                                if (err) {
                                    vscode.window.showErrorMessage('打开 wxFormBuilder 失败: ' + err.message);
                                }
                            });
                        } else {
                            vscode.window.showErrorMessage('未选择 wxFormBuilder.exe，无法打开 fbp 文件。');
                        }
                    });
                    return;
                }
            }
            // 启动 wxFormBuilder 打开 fbp 文件
            cp.execFile(wxFormBuilderPath, [fileUri.fsPath], (err) => {
                if (err) {
                    vscode.window.showErrorMessage('打开 wxFormBuilder 失败: ' + err.message);
                }
            });
    });
    context.subscriptions.push(disposable);
}

function deactivate() {}

module.exports = {
    activate,
    deactivate
};